package com.xqt.base.service;

import java.util.List;
import java.util.UUID;

import com.xqt.base.dao.BaseDao;
import com.xqt.base.entity.BaseEntity;
import com.xqt.base.entity.Operator;
import com.xqt.base.page.PageInfo;
import com.xqt.core.ClassReflectUtil;
import com.xqt.enums.Deleted;
import com.xqt.inters.service.base.IBaseService;
import com.xqt.util.DateUtils;
import com.xqt.util.SessionLocalUtils;

public abstract class BaseService<T> implements IBaseService<T> {

    public abstract BaseDao<T> getDao();
	
    
    @Override
	public void add(T t) throws Exception {
		// 设置主键.字符类型采用UUID,数字类型采用自增
		ClassReflectUtil.setFieldValue(t, "id", UUID.randomUUID().toString());
		// 设置新建人与新建日期
		ClassReflectUtil.setFieldValue(t, "createDate", DateUtils.getDate());
		Operator operator = SessionLocalUtils.getOperator();
		if (operator != null && operator.getMember() != null) {
			ClassReflectUtil.setFieldValue(t, "createBy", operator.getMember()
					.getId());
		}
		// 设置删除标记位为正常
		ClassReflectUtil.setFieldValue(t, "delFlag", Deleted.DELETED_NO.key);

		// 设置更新人与更新日期
		preUpdate(t);
		getDao().add(t);
	}

    public void preAdd(T t) throws Exception {
    	// 设置主键.字符类型采用UUID,数字类型采用自增
    			ClassReflectUtil.setFieldValue(t, "id", UUID.randomUUID().toString());
    			// 设置新建人与新建日期
    			ClassReflectUtil.setFieldValue(t, "createDate", DateUtils.getDate());
    			Operator operator = SessionLocalUtils.getOperator();
    			if (operator != null && operator.getMember() != null) {
    				ClassReflectUtil.setFieldValue(t, "createBy", operator.getMember()
    						.getId());
    			}
    			// 设置删除标记位为正常
    			ClassReflectUtil.setFieldValue(t, "delFlag", Deleted.DELETED_NO.key);
	}
    
	public void preUpdate(T t) throws Exception {
		// 设置更新人与更新日期
		ClassReflectUtil.setFieldValue(t, "updateDate", DateUtils.getDate());
		Operator operator = SessionLocalUtils.getOperator();
		if (operator != null && operator.getMember() != null) {
			ClassReflectUtil.setFieldValue(t, "updateBy", operator.getMember().getId());
		}
	}

	public void preInsert(BaseEntity entity) {
		entity.setId(UUID.randomUUID().toString());
		entity.setCreateDate(DateUtils.getDate());
		entity.setUpdateDate(DateUtils.getDate());
		Operator operator = SessionLocalUtils.getOperator();
		if (operator != null && operator.getMember() != null) {
			entity.setCreateBy(operator.getMember().getId());
			entity.setUpdateBy(operator.getMember().getId());
		}
		entity.setDelFlag(Deleted.DELETED_NO.getKey());
	}

	public void preUpdate(BaseEntity entity) {
		entity.setUpdateDate(DateUtils.getDate());
		Operator operator = SessionLocalUtils.getOperator();
		if (operator != null && operator.getMember() != null) {
			entity.setUpdateBy(operator.getMember().getId());
		}
	}

	@Override
	public void delete(Object... ids) {
		if (ids == null || ids.length < 1) {
			return;
		}
		for (Object id : ids) {
			getDao().delete(id);
		}
		
	}

	@Override
	public T queryById(Object id) {
		return getDao().queryById(id);
	}

	@Override
	public void updateBySelective(T t) throws Exception {
		getDao().updateBySelective(t);
	}

	@Override
	public int queryByCount(BaseEntity entity) {
		return getDao().queryByCount(entity);
	}

	@Override
	public List<T> queryByList(BaseEntity entity) {
		return getDao().queryByList(entity);
	}

	@Override
	public List<T> queryPageList(BaseEntity entity) {
		return getDao().queryPageList(entity);
	}

	@Override
	public PageInfo<T> queryPageInfo(BaseEntity entity) {
		return getDao().queryPageInfo(entity);
	}

	@Override
	public PageInfo<T> selectPageListCustom(T entity, String countMethod, String sqlMethod, int pageIndex,int pageSize) {
		return getDao().selectPageListCustom(entity, countMethod, sqlMethod, pageIndex, pageSize);
	}

	@Override
	public PageInfo<T> selectPageByEntity(BaseEntity entity, String countMethod, String sqlMethod) {
		return getDao().selectPageByEntity(entity, countMethod, sqlMethod);
	}

	@Override
	public <E, F> List<E> selectEntityList(F entity, String sqlMethod) {
		return getDao().selectEntityList(entity, sqlMethod);
	}
	
}
